package com.shop.admin.config;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shop.entity.pojo.Cost;
import com.shop.entity.pojo.Style;
import com.shop.entity.pojo.Wares;
import com.shop.admin.mapper.CostMapper;
import com.shop.admin.mapper.StyleMapper;
import com.shop.admin.mapper.WaresMapper;
import com.shop.admin.service.StyleService;
import com.shop.admin.service.WaresService;
import com.shop.utils.common.enums.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;

import java.util.List;

@Configuration
@Slf4j
public class CheckWaresConfig {


    @Autowired
    private WaresMapper waresMapper;
    @Autowired
    private WaresService waresService;
    @Autowired
    private StyleMapper styleMapper;
    @Autowired
    private StyleService styleService;
    @Autowired
    private CostMapper costMapper;

    @Async
    public void checkWares(){
        log.info("检查商品是否存在款式与价格");
        List<Wares> waresList = waresService.list();
        Wares wares=new Wares();
        wares= wares.builder().state(Message.Exception).build();
        for (Wares checkWares : waresList) {
            List<Style> styles = styleMapper.selectList(Wrappers.<Style>lambdaQuery().eq(Style::getWaresId, checkWares.getId()).eq(Style::getState,Message.Put));
            List<Cost> costs = costMapper.selectList(Wrappers.<Cost>lambdaQuery().eq(Cost::getStyleId, checkWares.getId()).eq(Cost::getState,Message.Put));
            if (CollectionUtils.isEmpty(styles)||styles.size()==Message.Check_Int_Null||CollectionUtils.isEmpty(costs)||costs.size()==Message.Check_Int_Null) {
                log.info("无款式、无价格--异常商品");
                int updateWares = waresMapper.update(wares, Wrappers.<Wares>lambdaUpdate().eq(Wares::getId, checkWares.getId()));
                if (updateWares==Message.Check_Int_False) {
                    throw new RuntimeException("检查商品是否有款式与价格存在报错");
                }
            }
        }
    }

    @Async
    public void checkStyle(){
        log.info("检查款式是否有价格");
        List<Style> styleList = styleService.list();
        Style style=new Style();
        style=style.builder().state(Message.Exception).build();
        for (Style checkStyle : styleList) {
            List<Cost> costs = costMapper.selectList(Wrappers.<Cost>lambdaQuery().eq(Cost::getStyleId, checkStyle.getId()).eq(Cost::getState,Message.Put));
            if (CollectionUtils.isEmpty(costs)||costs.size()==Message.Check_Int_Null) {
                log.info("无价格--异常款式");
                int updateStyle = styleMapper.update(style, Wrappers.<Style>lambdaUpdate().eq(Style::getId, checkStyle.getId()));
                if (updateStyle==Message.Check_Int_False) {
                    throw new RuntimeException("检查商品款式是否有价格存在报错");
                }
            }
        }
    }

}
